/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfocompany.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfocompany.entity.CampusinfoCompany;
import com.lczyfz.campusinfo.modules.campusinfocompany.service.CampusinfoCompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfocompany.entity.CampusinfoFundReq;
import com.lczyfz.campusinfo.modules.campusinfocompany.service.CampusinfoFundReqService;

import java.util.List;

/**
 * 资金申请Controller
 * @author zms
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfocompany/campusinfoFundReq")
public class CampusinfoFundReqController extends BaseController {

	@Autowired
	private CampusinfoFundReqService campusinfoFundReqService;

	@Autowired
	private CampusinfoCompanyService campusinfoCompanyService;
	@ModelAttribute
	public CampusinfoFundReq get(@RequestParam(required=false) String id) {
		CampusinfoFundReq entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoFundReqService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoFundReq();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfocompany:campusinfoFundReq:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoFundReq campusinfoFundReq, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoFundReq> page = campusinfoFundReqService.findPage(new Page<CampusinfoFundReq>(request, response), campusinfoFundReq);
		List<CampusinfoFundReq> campusinfoFundReqs =campusinfoFundReqService.findList(campusinfoFundReq);
		model.addAttribute("campusinfoFundReqs",campusinfoFundReqs);
		model.addAttribute("page", page);
		return "modules/campusinfocompany/campusinfoFundReqList";
	}

	@RequiresPermissions("campusinfocompany:campusinfoFundReq:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoFundReq campusinfoFundReq, Model model) {
		CampusinfoCompany campusinfoCompany = new CampusinfoCompany();
		List<CampusinfoCompany> campusinfoCompanies = campusinfoCompanyService.findList(campusinfoCompany);
		model.addAttribute("campusinfoCompanies",campusinfoCompanies);
		model.addAttribute("campusinfoFundReq", campusinfoFundReq);
		return "modules/campusinfocompany/campusinfoFundReqForm";
	}

	@RequiresPermissions("campusinfocompany:campusinfoFundReq:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoFundReq campusinfoFundReq, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoFundReq)){
			return form(campusinfoFundReq, model);
		}
		campusinfoFundReqService.save(campusinfoFundReq);
		addMessage(redirectAttributes, "答复成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/campusinfoFundReq/?repage";
	}
	
	@RequiresPermissions("campusinfocompany:campusinfoFundReq:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoFundReq campusinfoFundReq, RedirectAttributes redirectAttributes) {
		campusinfoFundReqService.delete(campusinfoFundReq);
		addMessage(redirectAttributes, "删除资金申请成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/campusinfoFundReq/?repage";
	}
	@RequiresPermissions("campusinfocompany:campusinfoFundReq:edit")
	@RequestMapping(value = "status")
	public String status(CampusinfoFundReq campusinfoFundReq, RedirectAttributes redirectAttributes) {
		campusinfoFundReq.setReqStatus("2");
		campusinfoFundReqService.status(campusinfoFundReq);
		addMessage(redirectAttributes, "资金申请审核成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/campusinfoFundReq/?repage";
	}
	@RequiresPermissions("campusinfocompany:campusinfoFundReq:edit")
	@RequestMapping(value = "cashier")
	public String cashier(CampusinfoFundReq campusinfoFundReq, RedirectAttributes redirectAttributes) {
		CampusinfoFundReq getcampusinfoFundReq = campusinfoFundReqService.get(campusinfoFundReq);
		getcampusinfoFundReq.setReqStatus("3");
		//campusinfoFundReqService.status(campusinfoFundReq);
		CampusinfoCompany campusinfoCompany = new CampusinfoCompany();
		campusinfoCompany.setId(campusinfoFundReq.getCompanyId());
		CampusinfoCompany getcompany = campusinfoCompanyService.get(campusinfoCompany);
		Integer req = Integer.parseInt(getcompany.getReqRemainder()); //获取当前的提取金额
		Integer take =  Integer.parseInt(campusinfoFundReq.getProvidefund()); //获取发放金额
		Integer cur = Integer.parseInt(getcompany.getCurRemainder()); //当前余额
		Integer ava = Integer.parseInt(getcompany.getAvaRemainder()); //可用余额
        Integer curtotal = cur - take;
        Integer avatotal = ava - take;
		Integer total = req+take;  //获取的总的提取金额
		getcompany.setReqRemainder(total.toString());
		getcompany.setAvaRemainder(avatotal.toString());
		getcompany.setCurRemainder(curtotal.toString());
		campusinfoCompanyService.save(getcompany);
		getcampusinfoFundReq.setProvidefund(take.toString());
		campusinfoFundReqService.save(getcampusinfoFundReq);
		addMessage(redirectAttributes, "资金申请出纳成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/campusinfoFundReq/?repage";
	}
}